package readData;

import Types.DrugDDIFile;
import Types.Entity;
import Types.Sentence;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PreProcess {

    public void preProcessSentences(List<DrugDDIFile> files) {
        for (int i = 0; i < files.size(); i++) {
            preProcessSentences(files.get(i));
        }
    }

    public void preProcessSentences(DrugDDIFile file) {
        List<Sentence> lstSentence = file.getLstSentence();
        for (int i = 0; i < lstSentence.size(); i++) {
            lstSentence.get(i).setSimplifiedText(simplify(lstSentence.get(i)));
        }
    }

    public String simplify(Sentence sentence) {
        String simplifiedText = "";
        //replace drug names with a unique value
        List<Entity> lstEntities = sentence.getEntitiesList();
        int startIndex = 0;
        if (lstEntities.isEmpty()) {
            simplifiedText = sentence.getText();
        } else {
            for (int i = 0; i < lstEntities.size(); i++) {
                Entity entity = lstEntities.get(i);
                simplifiedText += sentence.getText().substring(startIndex, entity.getCharOffset_start()) + "replaceddrug";// + sentence.getText().substring(entity.getCharOffset_end() + 1, sentence.getText().length());
                startIndex = entity.getCharOffset_end() + 1;
            }
            simplifiedText += sentence.getText().substring(startIndex);
        }

        simplifiedText = simplifiedText.toLowerCase();

        //replace numbers with a unique value
        String regex = "\\s+\\p{Punct}*\\d+\\.?\\d*\\p{Punct}*\\s*";
        Pattern pattern = Pattern.compile(regex);
        while (true) {
            Matcher matcher = pattern.matcher(simplifiedText);
            if (matcher.find()) {
                simplifiedText = simplifiedText.substring(0, matcher.start()) + simplifiedText.substring(matcher.start(), matcher.end()).replaceAll("\\d+", "replacednumber") + simplifiedText.substring(matcher.end());
            } else {
                break;
            }
        }

        //stemming
        Stemmer stem = new Stemmer();
        simplifiedText = stem.StemASentences(simplifiedText);
        //simplifiedText = lemma(simplifiedText);

        //tokenize the text
        Tokenizer tokenizer = new Tokenizer();
        simplifiedText = tokenizer.tokenize(simplifiedText);

//        //delete special characters
        simplifiedText = simplifiedText.replaceAll(",", " ");
        simplifiedText = simplifiedText.replaceAll("'", " ");
//        simplifiedText = simplifiedText.replaceAll(";", " ");
//        simplifiedText = simplifiedText.replaceAll(":", " ");
//        simplifiedText = simplifiedText.replaceAll("-", " ");
        simplifiedText = simplifiedText.replaceAll("%", " ");
//        simplifiedText = simplifiedText.replaceAll("\\(", " ");
//        simplifiedText = simplifiedText.replaceAll("\\)", " ");
//        simplifiedText = simplifiedText.replaceAll("\\.$", "");
//        simplifiedText = simplifiedText.replaceAll("\\[", "");
//        simplifiedText = simplifiedText.replaceAll("\\]", "");
//        simplifiedText = simplifiedText.replaceAll("\\{", "");
//        simplifiedText = simplifiedText.replaceAll("\\}", "");
        simplifiedText = simplifiedText.replaceAll("  ", " ");
        simplifiedText = simplifiedText.replaceAll("  ", " ");
        return simplifiedText;
    }
}
